"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 服务端工具
Case Name   : 无admin权限用例执行flush binary logs
Create At   : 2022/10/19
@zou_jialiang0501129488
Description :
    1.创建兼容B数据库并创建普通用户
    2.设置guc参数archive_mode=on并重启数据库
    3.执行flush binary logs操作
    4.清理环境
Expect      :
    1.兼容B库创建成功，用户创建成功
    2.guc参数设置成功
    3.执行失败，提示权限不足
    4.清理成功
History     :
    modified by @zou_jialiang050 at 2023-6-8:修改数据库重启使用公共方法，防止Degraded状态断言失败
"""
import os
import re
import unittest

from yat.test import Node
from yat.test import macro

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger

Constant = Constant()


class Tools(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.common = Common()
        self.primary_root_node = Node('PrimaryRoot')
        self.primary_node = Node('PrimaryDbUser')
        self.comsh_p = CommonSH("PrimaryDbUser")
        self.archive_path = os.path.join(macro.PG_XLOG_PATH, 'archive_status')

    def test_flush(self):
        text = '-----step1:创建兼容B库 expect:成功-----'
        self.log.info(text)
        sql_cmd = f"drop database if exists db_flush_case0002;" \
                  f"create database db_flush_case0002 " \
                  f"dbcompatibility ='B' encoding ='UTF8';"
        self.log.info(sql_cmd)
        sql_res = self.comsh_p.execut_db_sql(sql_cmd, dbname='postgres')
        self.log.info(sql_res)
        self.assertIn(Constant.CREATE_DATABASE_SUCCESS, sql_res,
                      '执行失败' + text)

        sql_cmd = f"drop user if exists u_db_flush_case0002;" \
                  f"create user u_db_flush_case0002 " \
                  f"with password '{macro.COMMON_PASSWD}'"
        self.log.info(sql_cmd)
        sql_res = self.comsh_p.execut_db_sql(sql_cmd,
                                             dbname='db_flush_case0002')
        self.log.info(sql_res)
        self.assertIn(Constant.CREATE_ROLE_SUCCESS_MSG, sql_res, '用户创建失败')

        text = '-----step2:设置guc参数archive_mode=on并重启数据库 expect:成功-----'
        self.log.info(text)
        res = self.comsh_p.execute_gsguc('set', Constant.GSGUC_SUCCESS_MSG,
                                         param='archive_mode=on',
                                         get_detail=True)
        self.assertIn(Constant.GSGUC_SUCCESS_MSG, res, 'guc设置失败')
        restart = self.comsh_p.restart_db_cluster(get_detail=True)
        self.assertIn(Constant.GS_OM_START_SUCCESS_MSG, restart, '数据库重启失败')

        text = '-----step3:执行flush binary logs操作 expect:成功-----'
        self.log.info(text)
        sql_cmd = 'flush binary logs;'
        conn = f'-U u_db_flush_case0002 -W {macro.COMMON_PASSWD}'
        flush_res = self.comsh_p.execut_db_sql(sql_cmd, sql_type=conn,
                                               dbname='db_flush_case0002')
        self.log.info(flush_res)
        self.assertIn('ERROR:  Must be system admin or operator admin',
                      flush_res, 'flush错误')

    def tearDown(self):
        text = "-----step4:清理环境 expect:成功-----"
        self.log.info(text)
        sql_cmd = f"drop database if exists db_flush_case0002;"
        self.log.info(sql_cmd)
        sql_res = self.comsh_p.execut_db_sql(sql_cmd, dbname='postgres')
        self.log.info(sql_res)
        sql_cmd = f"drop user if exists u_db_flush_case0002;"
        self.log.info(sql_cmd)
        user_res = self.comsh_p.execut_db_sql(sql_cmd, dbname='postgres')
        self.log.info(user_res)

        res = self.comsh_p.execute_gsguc('set', Constant.GSGUC_SUCCESS_MSG,
                                         param='archive_mode=off',
                                         get_detail=True)
        self.comsh_p.restart_db_cluster()
        status = self.comsh_p.get_db_cluster_status(param='status')
        self.assertIn(Constant.DROP_DATABASE_SUCCESS, sql_res, '数据库删除失败')
        self.assertIn(Constant.DROP_ROLE_SUCCESS_MSG, user_res, '用户删除失败')
        self.assertIn(Constant.GSGUC_SUCCESS_MSG, res, 'guc设置失败')
        self.assertTrue(status, '数据库状态异常')
        self.log.info(f'-----{os.path.basename(__file__)} finish-----')
